﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using test3.Hubs;

namespace test3.AutoMapper
{
    public class CustomProfile : Profile
    {
        /// <summary>
        /// 配置构造函数，用来创建关系映射
        /// </summary>
        public CustomProfile()
        {
            CreateMap<student, Student>()
                            .AfterMap((source, dto) =>
                            {
                                dto.Sex = Convert.CSex(source.Sex);
                                dto.Age = Convert.CDateTime(source.Age, DateTimeFormat.ToLongDateString);
                                dto.State = Convert.CState(source.State);
                                dto.CreateTime = Convert.CDateTime(source.CreateTime, DateTimeFormat.ToLongDateString);
                            });

            CreateMap<studentschool, StudentSchool>()
                .AfterMap((source, dto) =>
                {
                    dto.Sex = Convert.CSex(source.Sex);
                    dto.Age = Convert.CDateTime(source.Age, DateTimeFormat.ToLongDateString);
                    dto.State = Convert.CState(source.State);
                    dto.CreateTime = Convert.CDateTime(source.CreateTime, DateTimeFormat.ToLongDateString);
                });
        }


    }

    public class Convert
    {
        public static String CSex(Boolean s)
        {
            if (s == true) return "男";
            else return "女";
        }

        public static String CState(Boolean s)
        {
            if (s == true) return "启用";
            else return "禁用";
        }

        public static String CDateTime(DateTime d, DateTimeFormat format)
        {
            switch(format)
            {
                case DateTimeFormat.ToLongDateString:
                    return d.ToString("yyyy-MM-dd");
                case DateTimeFormat.ToLongTimeString:
                    return d.ToString("HH:mm:ss");
                case DateTimeFormat.ToShortDateString:
                    return d.ToShortDateString();
                case DateTimeFormat.ToShortTimeString:
                    return d.ToShortTimeString();
                default:
                    return d.ToString();
            }
        }
    }
}
